<template>
  <div>
    <!-- 搜索区域 -->
    <a-form layout="horizontal">
      <a-row>
        <div>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="用户信息"
              :labelCol="{span: 4}"
              :wrapperCol="{span: 18, offset: 2}">
              <a-select
                showSearch
                :value="userName"
                placeholder="input search text"
                :defaultActiveFirstOption="false"
                :showArrow="false"
                :filterOption="false"
                @search="handleSearch"
                @change="handleChange"
                :notFoundContent="null"
              >
                <a-select-option v-for="d in resultList" :key="d.userid">{{d.userName}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </div>
      </a-row>
    </a-form>
    <a-table ref="TableInfo"
             :columns="columns"
             :dataSource="data"
             :pagination="pagination"
             :loading="loading"
    >
    </a-table>
  </div>
</template>

<script>
export default {
  data () {
    return {
      data: [],
      username: '',
      searchList: [],
      resultList: [],
      foodList: [],
      loading: false,
      columns: [
        {title: '商品ID', dataIndex: 'itemid', ellipsis: true},
        {title: '商品名称', dataIndex: 'itemName', ellipsis: true},
        {title: '商品描述', dataIndex: 'remark'},
        // {title: 'image', dataIndex: 'image'},
        {title: '推荐值', dataIndex: 'pref'},
        {title: '启动状态', dataIndex: 'status'}
      ],
      pagination: {
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      }
    }
  },
  methods: {
    reset () {
    },
    handleSearch (value) {
      this.resultList = []
      for (let bean of this.searchList) {
        if (bean.userName.indexOf(value) !== -1) {
          this.resultList.push(bean)
        }
      }
    },
    handleChange (value, option) {
      this.userName = value
      this.listData(option.key)
    },
    listUser () {
      this.$get(`item/userAll`).then((r) => {
        console.log(r.data)
        this.searchList = r.data
      })
    },
    listData (userid) {
      let params = {}
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.TableInfo.pagination.current = this.paginationInfo.current
        this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
        params.pageSize = this.paginationInfo.pageSize
        params.pageNum = this.paginationInfo.current
      } else {
        // 如果分页信息为空，则设置为默认值
        params.pageSize = this.pagination.defaultPageSize
        params.pageNum = this.pagination.defaultCurrent
      }
      this.$get('item/user/item', {
        ...params,
        userid
      }).then((r) => {
        console.log(r)
        this.data = r.data.rows
        const pagination = {...this.pagination}
        pagination.total = r.data.total
        this.pagination = pagination
        // 数据加载完毕，关闭loading
        this.loading = false
      })
    }
  },
  mounted () {
    this.listUser()
  }
}
</script>

<style scoped>

</style>
